/**
 * 北京中流科技有限公司 版权所有
 * http://www.chinasoa.com.cn
 */
package cn.com.chinasoa.course.subscribe;

import java.io.IOException;

import javax.persistence.EntityManager;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;

import cn.com.chinasoa.course.subscribe.model.TrainingSubject;
import cn.com.chinasoa.course.subscribe.util.DataTypeUtil;
import cn.com.chinasoa.course.subscribe.util.EntityManagerHelper;

import com.x4ee.soa.portal.framework.core.ActionConfig;
import com.x4ee.soa.portal.framework.core.ActionFilter;
import com.x4ee.soa.portal.framework.core.BaseRequestFilter;

/**
 * @author 刘洋(liuyang@chinasoa.com.cn)
 * 2007-11-5下午03:02:31
 */
public class TrainingSubjectOperateActionFilter extends BaseRequestFilter 
	implements ActionFilter {

	private static String SUCCESS = "success";
	private static String FAILED = "error";

	public String process(ActionRequest actionRequest, ActionResponse actionResponse,
			ActionConfig actionConfig) throws PortletException, IOException {

		boolean success = false;

		long trainingSubjectId = DataTypeUtil.parseLong(actionRequest.getParameter("trainingSubjectId"));
		String cmd = actionRequest.getParameter("cmd");

		String code = actionRequest.getParameter("code");
		String summary = actionRequest.getParameter("summary");
		String period = actionRequest.getParameter("period");
		String startTime = actionRequest.getParameter("startTime");

		String endTime = actionRequest.getParameter("endTime");
		String description = actionRequest.getParameter("description");
		String price = actionRequest.getParameter("price");
		String type = actionRequest.getParameter("type");
		
		if((trainingSubjectId>0)&&(cmd!=null)&&(code!=null)&&(summary!=null)&&(period!=null)
				&&(startTime!=null)&&(endTime!=null)&&(description!=null)&&(price!=null)&&(type!=null)){

			float periodFloat = DataTypeUtil.parseFloat(period);
			int priceInt = DataTypeUtil.parseInt(price);
			
			if((periodFloat>0)&&(priceInt>=0)){
				if(cmd.equals("edit")){
					
					EntityManager em = EntityManagerHelper.getEntityManager();
					em.getTransaction().begin();
					TrainingSubject vTrainingSubject = em.find(TrainingSubject.class, trainingSubjectId);
					if(vTrainingSubject!=null){
						vTrainingSubject.setCode(code);
						vTrainingSubject.setSummary(summary);
						vTrainingSubject.setPeriod(periodFloat);				
						vTrainingSubject.setStartTime(DataTypeUtil.parseTimeAMPM(startTime));
						vTrainingSubject.setEndTime(DataTypeUtil.parseTimeAMPM(endTime));
						vTrainingSubject.setDescription(description);
						vTrainingSubject.setPrice(priceInt);
						vTrainingSubject.setType(type);
					}
					em.getTransaction().commit();
					EntityManagerHelper.closeEntityManager();
	
					success = true;
				}
			}
		}else{
			if((trainingSubjectId>0)&&(cmd!=null)&&(cmd.equals("delete"))){
				EntityManager em = EntityManagerHelper.getEntityManager();
				em.getTransaction().begin();
				TrainingSubject vTrainingSubject = em.find(TrainingSubject.class, trainingSubjectId);
				if(vTrainingSubject!=null){
					em.remove(vTrainingSubject);
				}
				em.getTransaction().commit();
				EntityManagerHelper.closeEntityManager();
				success = true;				
			}else{
				success = false;
			}
		}

		if(success)return SUCCESS;
		else return FAILED;
	}
}
